import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import vueSetupExtend from 'unplugin-vue-setup-extend-plus/vite';
// @ts-ignore
import { createSvgIconsPlugin } from "./build/vite-plugin-svg-icons/index.mjs";
// import compression from "vite-plugin-compression";
import legacy from '@vitejs/plugin-legacy'
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { fileURLToPath, URL } from "node:url";
import path from "path";
// https://vitejs.dev/config/
function resolve(dir: string) {
  return path.resolve(process.cwd(), dir);
}
const RandomId = Math.ceil((1000 + Math.random()) * Math.random());
const __VITE_APP_LOGO_URL__ = JSON.stringify("/src/icons/svg/xzrjgj.svg");
export default defineConfig(({ mode }) => {
  const { VITE_APP_DIST_ORIGIN } = loadEnv(mode, process.cwd(), 'VITE_');
  return {
    base: "./",
    build: {
      outDir: VITE_APP_DIST_ORIGIN,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      }
    },
    plugins: [
      vue(),
      vueJsx(),
      vueSetupExtend({
        enableAutoExpose: true
      }),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [resolve("src/icons/svg")],
        // 指定symbolId格式
        symbolId: "icon" + "-[name]",
        customDomId: "__fy_sidebar__icons__dom__"
      }),
      AutoImport({
        resolvers: [ElementPlusResolver({ importStyle: "sass" })],
      }),
      Components({
        resolvers: [ElementPlusResolver({ importStyle: "sass" })],
      }),
      // compression({
      //   threshold: 10240, // 对超过10KB的文件进行压缩
      //   algorithm: "gzip", // 使用gzip压缩
      //   ext: ".gz" // 压缩后文件的扩展名
      // })
      legacy({
        targets: ['chrome 89'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        renderLegacyChunks: true,
        polyfills: ['es.symbol', 'es.promise']
      })
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
        "~@": fileURLToPath(new URL("./src", import.meta.url))
      }
    },
    define: {
      __VITE_APP_LOGO_URL__,
      __INTLIFY_JIT_COMPILATION__: true,
      __VITE_APP_RANDOMID__: RandomId
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/variables.scss" as *;@use "@/styles/mixins.scss" as *;$LogoURL: ${__VITE_APP_LOGO_URL__};`
        }
      }
    },
    server: {
      host: "0.0.0.0",
      port: 82
    }
  }
});
